package edu.wit.system.controller;

import edu.wit.common.result.R;
import edu.wit.common.security.service.TokenService;
import edu.wit.common.web.PageResult;
import edu.wit.common.web.Result;
import edu.wit.system.domain.WorkflowType;
import edu.wit.system.domain.dto.AffairsWorkflowDto;
import edu.wit.system.domain.vo.AffairsWorkflowVo;
import edu.wit.system.mapper.AffairsApplyWorkflowMapper;
import edu.wit.system.service.AffairsWorkflowService;
import edu.wit.system.service.WorkflowTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author lee
 */
@RestController
@RequestMapping("/sys/affairsWorkflow")
@Api(tags = "流程管理")
@Validated
public class AffairsWorkflowController {
    @Autowired
    private AffairsWorkflowService affairsWorkflowService;

    @Autowired
    private WorkflowTypeService workflowTypeService;

    @Autowired
    private AffairsApplyWorkflowMapper affairsApplyWorkflowMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 我的申请列表
     */
    @ApiOperation(value = "获取列表",httpMethod = "GET")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "pageNum",value = "页码",required = true,dataType = "Integer",paramType = "form",defaultValue = "1"),
		@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,dataType = "Integer",paramType = "form",defaultValue = "10"),
        @ApiImplicitParam(name="orderByColumn" ,value="排序行",dataType = "String",paramType = "query"),
        @ApiImplicitParam(name="isAsc" ,value="排序的方向 desc 或者 asc",dataType = "String",paramType = "query"),
        @ApiImplicitParam(name="workflowNodeName" ,value="流程名称",dataType = "String",paramType = "query"),
        @ApiImplicitParam(name="workflowStatus" ,value="流程状态",dataType = "String",paramType = "query"),
    })
    @GetMapping("/getTableList")
    public PageResult<AffairsWorkflowDto> getTableList(@RequestParam(required = false) @ApiIgnore Map<String, Object> params,
                                                       @RequestHeader String projectId,
                                                       @RequestHeader(required = false) String serviceCorporationId){

//        String username = manager.getUserFromToken(ServletUtils.getRequest().getHeader("token"));
//        User user = affairsApplyWorkflowMapper.selectUserByUsername(username);

//        String projectAdminId = null;
//        if (user.getPid().equals("0")){
//            projectAdminId = user.getId();
//        } else {
//            projectAdminId = user.getPid();
//        }
//        params.put("projectId",Long.parseLong(projectAdminId));

        params.put("projectId",projectId);
        params.put("corporation",serviceCorporationId);
        return affairsWorkflowService.queryPage(params);
    }

    /**
     * 我的审批列表
     */
    @ApiOperation(value = "根据projectId获取流程定义列表",httpMethod = "GET")
    @GetMapping("/getList")
    @ApiImplicitParams({
        @ApiImplicitParam(name="projectId" ,value="项目ID",dataType = "String",paramType = "query"),
    })
    public Result<List<AffairsWorkflowDto>> getList(@RequestParam @ApiIgnore Map<String, Object> params){
       return Result.success(affairsWorkflowService.getList(params), "操作成功");
    }

    /**
     * 信息
     */
    @ApiOperation(value = "详细",httpMethod = "GET")
//    @ApiImplicitParams({
//    	@ApiImplicitParam(name = "id",value = "ID",required = true,dataType = "Long",paramType = "form"),
//    })
    @GetMapping("/info")
    public Result<AffairsWorkflowDto> info(@RequestParam("workflowId") Long workflowId){

//        System.out.println("流程id=" + workflowId);
		AffairsWorkflowDto affairsWorkflowDto = affairsWorkflowService.getById(workflowId);
        return Result.success(affairsWorkflowDto, "操作成功");
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存",httpMethod = "POST")
    @PostMapping("/save")
    //@Log(title = "保存")
    public Result<Boolean> saveInfo(@RequestBody @Valid AffairsWorkflowVo affairsWorkflowVo,
                                    @RequestHeader String projectId){
        affairsWorkflowVo.setProjectId(Long.parseLong(projectId));
//		affairsWorkflowService.saveInfo(affairsWorkflowVo);
        if (affairsWorkflowVo.getWorkflowType().equals("工资审批")) {
            affairsWorkflowService.saveWageWorkflow(affairsWorkflowVo);
        } else {
            affairsWorkflowService.saveInfo(affairsWorkflowVo);
        }
        return Result.success(true, "保存成功");
    }


    @ApiOperation(value = "保存工资流程",httpMethod = "POST")
    @PostMapping("/saveWageWorkflow")
    public Result<Boolean> saveWageWorkflow(@RequestBody @Valid AffairsWorkflowVo affairsWorkflowVo,
                                            @RequestHeader String projectId) {
        affairsWorkflowVo.setProjectId(Long.parseLong(projectId));
        boolean b = affairsWorkflowService.saveWageWorkflow(affairsWorkflowVo);

        if (b) {
            return Result.success(true,"保存成功");
        } else {
          return Result.failed("保存失败");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除",httpMethod = "POST")
   // @Log(title = "删除")
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody Long[] workflowIds){

//        System.out.println(workflowIds.toString());
		affairsWorkflowService.delete(Arrays.asList(workflowIds));
        return Result.success(true, "删除成功");
    }

    @ApiOperation(value = "获取流程类型",httpMethod = "GET")
    @GetMapping("/getWorkflowType")
    public R getWorkflowType() {
        List<WorkflowType> workType = workflowTypeService.getWorkType();
        return R.ok().data("workType",workType);
    }

    @ApiOperation(value="根据流程类型获取流程id")
    @GetMapping("/getWorkFlowIdByWorkType/{workflowType}")
    public R getWorkFlowIdByWorkType(@PathVariable String workflowType) {

        //有劳务公司则返回对应workflowId,无劳务公司则返回workflow集
        String id = affairsWorkflowService.selectWorkflowIdByWorkflowTypeAndPid(workflowType)+"";
        return R.ok().data("workflowId",id);
    }

    @ApiOperation(value = "根据workflowType,projectId和corporation获取流程")
    @GetMapping("/getWorkflowList/{workflowType}")
    public R getWorkflowList(@RequestHeader String projectId,
                             @RequestHeader(required = false) String serviceCorporationId,
                             @PathVariable String workflowType) {
        List<AffairsWorkflowDto> workflows = affairsWorkflowService.selectWorkflowList(projectId, serviceCorporationId,workflowType);
        return R.ok().data("workflows",workflows);
    }
}
